package com.atguigu.tingshu.account.api;

import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.login.TsLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.vo.account.AccountDeductVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@Tag(name = "用户账户管理")
@RestController
@RequestMapping("api/account/userAccount")
@SuppressWarnings({"all"})
public class UserAccountApiController {

	@Autowired
	private UserAccountService userAccountService;


	@TsLogin
	@Operation(summary = "获取账号可用金额")
	@GetMapping("getAvailableAmount")
	public Result<BigDecimal> getAvailableAmount(Long userId) {
		BigDecimal availableAmount = userAccountService.getAvailableAmount(userId);
		return Result.ok(availableAmount);
	}
	@TsLogin
	@Operation(summary = "检查和扣减账号余额")
	@PostMapping("checkAndDeduct")
	public  Result checkAndDeduct(@RequestBody AccountDeductVo accountDeductVo){
		Long row =userAccountService.checkAndDeduct(accountDeductVo);
		if (row==0){
			return  Result.fail();
		}
		return Result.ok();
	}
}

